package com.itheima.wallet.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.wallet.api.AccountService;
import com.itheima.wallet.common.domain.Account;
import com.itheima.wallet.common.domain.User;
import com.itheima.wallet.common.mapper.AccountMapper;
import com.itheima.wallet.user.api.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;


@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Reference
    private UserService userService;

    /**
     * 开户，创建一个新的账户
     * @param account
     * @return
     */
    public Boolean insertAccount(Account account) {
        return super.save(account);
    }

    /**
     * 根据Token 查询用户信息
     *
     * @param token
     * @return
     */
    @Override
    public Account queryAccount(String token) {
        User user = userService.queryUserByToken(token);
        if (user == null) {
            //Token校验不通过
            return null;
        }
        String account_id = user.getAccount_id();
        Account account = super.getById(account_id);
        //一个不知名bug
        account.setAccount_id(account_id);
        return account;
    }

    /**
     * 增加余额
     *
     * @param account
     * @param money
     * @return
     */
    @Override
    public Boolean addBalance(Account account, Double money) {
        account.setBalance(account.getBalance() + money);
        return super.updateById(account);
    }

    /**
     * 减少余额
     *
     * @param account
     * @param money
     * @return
     */
    @Override
    public Boolean reduceBalance(Account account, Double money) {
        account.setBalance(account.getBalance() - money);
        return super.updateById(account);
    }

}
